package edu.cdnu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.cdnu.dao.HomeworkDao;
import edu.cdnu.dao.StudentDao;
import edu.cdnu.pojo.HomeworkPojo;
import edu.cdnu.pojo.HomeworkStudentPojo;
import edu.cdnu.service.HomeworkService;

@Service
public class HomeworkServiceImpl implements HomeworkService{
	
	@Autowired
	private HomeworkDao homeworkDao;
	@Autowired
	private StudentDao studentDao;

	@Override
	public List<HomeworkPojo> selectHomeworkListByCourseId(int courseId) {
		return homeworkDao.selectHomeworkListByCourseId(courseId);
	}

	@Override
	public void addOrUpdateHomework(HomeworkPojo homeworkPojo) {
		if(homeworkPojo == null) {
			return ;
		}else if(homeworkPojo.getId() == null) {
			// id等于空 新增
			homeworkDao.insertHomework(homeworkPojo);
		}else {
			// 修改
			homeworkDao.updateHomeWorkByHomeworkPojo(homeworkPojo);
		}
	}

	@Override
	public void deleteHomeworkByHomeworkId(int homeworkId) {
		homeworkDao.deleteHomeworkByHomeworkId(homeworkId);
	}

	@Override
	public List<HomeworkStudentPojo> selectHomeworkListByHomeworkIdAndStudentId(Integer homeworkId, Integer studentId) {
		return homeworkDao.selectHomeworkListByHomeworkIdAndStudentId(homeworkId, studentId);
	}

	@Override
	public void insertHomeworkStudent(HomeworkStudentPojo homeworkStudentPojo) {
		// studentId 和 homeworkId 不能为空
		if(homeworkStudentPojo==null||homeworkStudentPojo.getStudentId()==null||homeworkStudentPojo.getHomeworkId()==null) {
			return ;
		}
		homeworkDao.insertHomeworkStudent(homeworkStudentPojo);
	}

	@Override
	public void insertOrUpdateHomeworkStudent(HomeworkStudentPojo homeworkStudentPojo) {
		// studentId 和 homeworkId 不能为空
		if(homeworkStudentPojo==null||homeworkStudentPojo.getStudentId()==null||homeworkStudentPojo.getHomeworkId()==null) {
			return ;
		}
		// 通过判断 学生-任务这个id来判断 是 新增还是修改
		if(homeworkStudentPojo.getId()==null) {
			// 没有id是新增
			homeworkDao.insertHomeworkStudent(homeworkStudentPojo);
		}else {
			// 有id是修改
			homeworkDao.updateHomeworkStudent(homeworkStudentPojo);
		}
	}

	@Override
	public Map<String,Object> selectHomeworkStudentListByHomeworkId(Integer homeworkId) {
		// 这个用来保存最终的数据
		Map<String, Object> map=new HashMap<>();
		// 先查询出来所以的作业列表
		List<HomeworkStudentPojo> homeworkStudentList=homeworkDao.selectHomeworkStudentListByHomeworkId(homeworkId);
		// 数据放到返回数据集中
		map.put("homeworkList", homeworkStudentList);
		
		// 参与学生的id，用这个保存
		List<Integer> studentIdList=new ArrayList<>();
		// 遍历这个对应关系，把学生id放到list
		homeworkStudentList.forEach((item)->{
			studentIdList.add(item.getStudentId());
			
		});
		// 判断学生id列表为不为空，有才查询
		if(!studentIdList.isEmpty()) {
			
			// 查询出来放到返回数据集中
			map.put("studentList",studentDao.selectStudentListByIdList(studentIdList));
		}
		return map;
	}

	@Override
	public List<Map<String, Object>> selectHomeworkStudentInfoListByHomeworkId(Integer homeworkId) {
		return homeworkDao.selectHomeworkStudentInfoListByHomeworkId(homeworkId);
	}
	
	

}
